package com.atcumt.QiAnXin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/* 
 * 
 * 2
 * 20 8
 * 1 2 3 2 6 5 2 1
 * 17 10
 * 1 4 5 7 10 8 7 17 2 8
 */
public class Main02 {
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        while (in.hasNext()) {
            int k = in.nextInt();
            int n = in.nextInt();
            int[] nums = new int[n];
            for (int i = 0; i < n; i++) {
                nums[i] = in.nextInt();
            }
            List<Integer> list = new ArrayList<>();
            getNum(list, k);
            int[] target = new int[list.size()];
            for (int i = 0; i < target.length; i++) {
                target[i] = list.get(i);
            }
            int i = solve(nums, target);
            System.out.println(i);
        }
        in.close();
    }

    public static int solve (int[] arr, int[] target) {
        Map<Integer, Integer> need = new HashMap<>();
        Map<Integer, Integer> window = new HashMap<>();
        for (int i : target) {
            need.put(i, need.getOrDefault(i, 0) + 1);
        }
        int left = 0, right = 0, valid = 0, len = Integer.MAX_VALUE;
        while (right < arr.length) {
            int c = arr[right];
            right++;
            if (need.containsKey(c)) {
                window.put(c, window.getOrDefault(c, 0) + 1);
                if (window.get(c).equals(need.get(c))) {
                    valid++;
                }
            }
            while (valid == need.size()) {
                if (right - left < len) {
                    len = right - left;
                }
                int d = arr[left];
                left++;
                if (need.containsKey(d)) {
                    if (window.get(d).equals(need.get(d))) {
                        valid--;
                    }
                    window.put(d, window.getOrDefault(d, 0) - 1);
                }
            }
        }
        return len;
    }


    private static void getNum(List<Integer> list, int num) {
        for (int i = 2; i < num; i++) {
            if (num % i == 0) {
                list.add(i);
                getNum(list, num / i);
                return;
            }
        }
        list.add(num);
    }
}
